package finalproject.model.rooms;

import finalproject.config.Randomizer;
import finalproject.config.Ressource;
import finalproject.model.items.LabItem;
import finalproject.model.player.Player;
import finalproject.model.studies.Lab;
import finalproject.model.studies.Topic;
import finalproject.view.StringManager;

/**
 * The lab room
 * @author Yassine Tijani
 *
 */
public class LabRoom extends Room {

	/**
	 * The associated lab session
	 */
	private Lab lab;

	/**
	 * @param description The labroom description
	 * @param lab The associated lab session
	 */
	public LabRoom(String description, Lab lab) {
		super(description,true);
		this.lab = lab;
	}



	@Override
	public boolean doActivity(Player p) {
		if(lab == null)
		{
			return false;
		}
		else if(lab.getTopic().equals(Topic.OOP) && p.getItemHolder().getWorkingManager().containskeyOf(this.lab.getTitle()) && p.getEnergy().getEnergy() != 0)
		{
			p.getItemHolder().getWorkingManager().addValue(new LabItem(this.lab));
			p.getEnergy().setEnergy(p.getEnergy().getEnergy()-1);
			return true;
		}
		else
		{
			return false;
		}
	}

	@Override
	public boolean leavable(Player p)
	{
		if(lab == null)
		{
			return true;
		}
		else if (!(lab.getTopic().equals(Topic.OOP)))
		{
			return true;
		}
		else if(!(p.getItemHolder().getWorkingManager().containskeyOf(this.lab.getTitle())))

		{
			return true;
		}
		else if(p.getItemHolder().getWorkingManager().getValueWith(this.lab.getTitle()) == null)
		{
			return false;
		}
		else
		{
			return true;
		}
	}



	@Override
	public boolean prepareActivity(Player p) {
		switch (Randomizer.getRandom().nextInt(5)) {
		case 0:
			this.lab = Ressource.otherLab;
			this.lab.setTopic(Topic.BIO);
			return false;
		case 1:
			this.lab = Ressource.otherLab;
			this.lab.setTopic(Topic.MATH);
			return false;
		case 2:
			this.lab = null;
			return false;
		case 3:
			this.lab = p.getBook().getUndoneLab(Ressource.listLab);
			return true;
		case 4:
			this.lab = null;
			return false;
		default:
			break;
		}
		return false;
	}

	@Override
	public String toString()
	{
		if(lab == null)
		{
			return super.toString() + " : " + StringManager.getString("emptyroom");
		}
		else if(!lab.getTopic().equals(Topic.OOP))
		{
			return super.toString() + " : " + StringManager.getString("othertopic")+ " " + lab.getTopic();
		}
		else
		{
			return super.toString() + " : "+ StringManager.getString("oop")+ " " + lab.getTitle();
		}
	}
}
